package cc.gu.tank.sprite;

import cc.gu.tank.Director;
import cc.gu.tank.enums.Direction;
import cc.gu.tank.enums.Group;
import cc.gu.tank.sound.SoundEffect;
import javafx.scene.image.Image;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: GuSongLiao
 * @Date: 2025/9/29 15:20
 * @Description: 子弹类
 */
public class Bullet extends Role {

    public Boom boom;

    private static Map<Direction, Image> ownBulletMap = new HashMap<Direction, Image>() {{
        put(Direction.UP, new Image("image/bullet-green-up.png"));
        put(Direction.DOWN, new Image("image/bullet-green-down.png"));
        put(Direction.LEFT, new Image("image/bullet-green-left.png"));
        put(Direction.RIGHT, new Image("image/bullet-green-right.png"));
    }};

    private static Map<Direction, Image> enemyBulletMap = new HashMap<Direction, Image>() {{
        put(Direction.UP, new Image("image/bullet-red-up.png"));
        put(Direction.DOWN, new Image("image/bullet-red-down.png"));
        put(Direction.LEFT, new Image("image/bullet-red-left.png"));
        put(Direction.RIGHT, new Image("image/bullet-red-right.png"));
    }};

    public Bullet(Tank tank) {
        super(tank.group == Group.OWNSIDE ? ownBulletMap.get(tank.direction) : enemyBulletMap.get(tank.direction), 0, 0, 0, 0);
        this.direction = tank.direction;
        if (direction == Direction.UP || direction == Direction.DOWN) {
            width = 10;
            height = 22;
            x = tank.x + tank.width / 2 - width / 2;
            y = tank.y;
        } else {
            width = 22;
            height = 10;
            x = tank.x;
            y = tank.y + tank.height / 2 - height / 2;
        }
        //增加部分像素，避免子弹刚生成时触发碰撞检测
        switch (direction) {
            case UP:
                y -= 2;
                break;
            case DOWN:
                y += 2;
                break;
            case LEFT:
                x -= 2;
                break;
            case RIGHT:
                x += 2;
                break;
        }
        this.group = tank.group;
        this.speed = 10;
        this.moveAble = true;
    }

    @Override
    public void move() {
        super.move();
        if (x < 0 || x > Director.WIDTH || y < 0 || y > Director.HEIGHT) {
            alive = false;
        }
    }

    @Override
    public Sprite impact(List<? extends Sprite> sprites) {
        Sprite sprite = super.impact(sprites);
        //碰撞检测成功，对方为子弹，则销毁
        if (sprite instanceof Tank && this.group != ((Tank) sprite).group || sprite instanceof Crate) {
            this.alive = false;
            sprite.alive = false;
            boom = new Boom(this);
        } else if (sprite instanceof Rock) {//子弹打石头，子弹消失
            this.alive = false;
            boom = new Boom(this);
        }
        return sprite;
    }


}
